package com.dms;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class DMSCheckNetworkBroadcastReceiver extends BroadcastReceiver {
	
	private DMSCheckNetworkService netState;
	
	String action;
	ConnectivityManager cManager;
	NetworkInfo mobile;
	NetworkInfo wifi;

	@Override
	public void onReceive(Context context, Intent intent) {
		
		action = intent.getAction();
		
		cManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 
		mobile = cManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
		wifi = cManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		
		if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
		{
		      ComponentName cn = new ComponentName(context.getPackageName(), DMSCheckNetworkService.class.getName());
		      ComponentName svcName = context.startService(new Intent().setComponent(cn));
		      
		      if (svcName == null)
		      {
		    	  Log.e("DMSCheckNetworkBroadcastReceiver", "Could not start service " + cn.toString());
		      }
		}
		
		if(action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
		{
			if(mobile.isConnected() || wifi.isConnected())
			{
				try
				{
					Log.e("DMSCheckNetworkBroadcastReceiver", "DMSCheckNetworkBroadcastReceiver()");
					netState = (DMSCheckNetworkService)DMSCheckNetworkService.mContext;
					netState.setNetState();
				}
				catch(NullPointerException npe)
				{
					npe.printStackTrace();
				}
			}
		}
	}

}
